package 堆栈与队列;

import java.util.LinkedList;
import java.util.Stack;

public class 逆波兰表达式求值 {
    public int evalRPN(String[] tokens) {
        Stack<String> stack = new Stack<>();
        for (String token : tokens) {
            if ("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token)) {
                String s1 = stack.pop();
                String s2 = stack.pop();
                String result = null;
                if ("+".equals(token)) {
                    result = String.valueOf(Integer.parseInt(s2) + Integer.parseInt(s1));
                } else if ("-".equals(token)) {
                    result = String.valueOf(Integer.parseInt(s2) - Integer.parseInt(s1));
                } else if ("*".equals(token)) {
                    result = String.valueOf(Integer.parseInt(s2) * Integer.parseInt(s1));
                } else {
                    result = String.valueOf(Integer.parseInt(s2) / Integer.parseInt(s1));
                }
                stack.push(result);
            } else {
                stack.push(token);
            }
        }
        return Integer.parseInt(stack.pop());
    }
}
